package com.qf.controller;

import com.qf.pojo.PatientPojo;
import com.qf.service.PatientService;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/patient")
@CrossOrigin
public class PatientController {
    @Autowired
    PatientService patientService;

    @GetMapping("/findAll")
    public ResultMsg findAll(@RequestParam(required = false, defaultValue = "1") Integer current, @RequestParam(required = false, defaultValue = "20") Integer pageSize) {
        return patientService.findAll(current, pageSize);
    }

    @PostMapping("/add")
    public ResultMsg add(@RequestBody PatientPojo patientPojo) {
        return patientService.addPatient(patientPojo);
    }

    @GetMapping("/delById/{patientId}")
    public ResultMsg delPatient(@PathVariable("patientId") String patientId) {
        return patientService.deletePatientByPatientId(patientId);
    }

    @GetMapping("/find/{keyword}")
    public ResultMsg findAll(@PathVariable("keyword") String keyword, @RequestParam(required = false, defaultValue = "1") Integer current, @RequestParam(required = false, defaultValue = "20") Integer pageSize) {
        return patientService.findAllByKeyWords(keyword, current, pageSize);
    }

    @PostMapping("/updateByPid")
    public ResultMsg updateByPid(String patientId, Integer userId) {
        return patientService.modifyDoctorForPatient(patientId, userId);
    }

    @GetMapping("/findByUid/{userId}")
    public ResultMsg findAll(@PathVariable("userId") Integer userId, @RequestParam(required = false, defaultValue = "1") Integer current, @RequestParam(required = false, defaultValue = "20") Integer pageSize) {
        return patientService.findAllByDoctorId(userId, current, pageSize);
    }

    @GetMapping("/findByDate")
    public ResultMsg findAll(String startTime, String endTime, @RequestParam(required = false, defaultValue = "1") Integer current, @RequestParam(required = false, defaultValue = "20") Integer pageSize) {
        return patientService.findAllByDate(startTime, endTime, current, pageSize);
    }

    @PostMapping("/login")
    public ResultMsg login(@RequestBody Map<String, String> map, HttpServletRequest request) {
        return patientService.login((String) map.get("patientName"), (String) map.get("patientTel"), request);
    }

    @GetMapping("/findByDay")
    public ResultMsg findByDay(@RequestParam(required = false, defaultValue = "-1") Integer patientStatus) {
        return patientService.findByDay(patientStatus);
    }

    @GetMapping("/updatePatientStatus")
    public ResultMsg updatePatientStatus(String patientId) {
        return patientService.updatePatientStatus(patientId);
    }

    @GetMapping("/findAllByStatus")
    public ResultMsg findAllByStatus(@RequestParam(required = false, defaultValue = "-1") Integer patientStatus) {
        return patientService.findByStatus(patientStatus);
    }

    @PostMapping("/register")
    public ResultMsg register(@RequestBody Map<String, String> map) {
        return patientService.register(map.get("patientName"), map.get("patientTel"));
    }

    @GetMapping("/logout")
    public ResultMsg logout(HttpServletRequest request) {
        return patientService.logout(request);
    }
}
